#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define BUFFSIZE 10240

	int main(int argc,char **argv)
{
	if(argc < 3)
	{
		fprintf(stderr,"Usage:%s <filename> <filename>",argv[0]);
		exit(1);
	}

	int fps,fpd;
	char buff[BUFFSIZE];
	ssize_t len = 0;
	ssize_t ret = 0;
	ssize_t pos = 0;
	
	fps = open(argv[1],O_RDONLY);
	fpd = open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0666);

	while(1)
	{
		len = read(fps,buff,BUFFSIZE);
		if(len < 0)
		{
			perror("read()");
			exit(1);
		}
		if(len == 0)
			break;
//ret和pos设置为了防止读入数据不完全
		pos = 0;
		while(len > 0)
		{
			ret = write(fpd,buff+pos,len);
			if(ret < 0)
			{
				perror("write()");
				exit(1);
			}
			pos += ret;
			len -= ret;
		}
	}

	off_t n;
	n = lseek(fps,0,SEEK_END);
	printf("%ld\n",n);

	close(fpd);
	close(fps);
	exit(0);
}
